package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.FoodMaterialAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.listener.TextWatcherAdapter;

import java.util.List;

import butterknife.BindView;

/**
 * 食材选择页面
 */
public class FoodMaterialActivity extends BaseActivity{
    public static final String TAG="FoodMaterialActivity";
    public static final String SRC_CLASS="SRC_CLASS";
    @BindView(R.id.e_gv)
    GridView gv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private FoodMaterialAdapter adapter;
    private TopbarHelper topbarHelper;
    private MenuFoodMaterialDao foodMaterialDao;


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_food_material_list;
    }

    @Override
    protected void initialView() {
        Log.e(TAG,"initialView");
        topbarHelper=new TopbarHelper(this);
        topbarHelper.setTitle("食材选择");
        topbarHelper.setReturnEnable(false);
        topbarHelper.setcon(R.drawable.arrow_left);
        adapter=new FoodMaterialAdapter(this);
        gv.setAdapter(adapter);
        foodMaterialDao=new MenuFoodMaterialDao(this);
        etSeach.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                super.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                adapter.setItems(foodMaterialDao.queryForAllByName(s.toString(),Constant.FOODS_TYPE_1));
                adapter.setItems(CookingMenuMangerHelper.getInstance(FoodMaterialActivity.this,
                        new CookingMenuDao(FoodMaterialActivity.this)).getStdMenuFoodMaterials(s.toString()));


            }

            @Override
            public void afterTextChanged(Editable s) {
//                adapter.setItems(CookingMenuMangerHelper.getInstance(FoodMaterialActivity.this,
//                        new CookingMenuDao(FoodMaterialActivity.this)).getStdMenuFoodMaterials(""));

            }
        });
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                handleItem(position);
            }
        });
    }



    private void handleItem(int position) {
        MenuFoodMaterial foodMaterial=adapter.getItem(position);
        Intent intent=new Intent();
        intent.putExtra(Constant.FOOD_NAME,foodMaterial.getFoodName());
        intent.putExtra(Constant.FOOD_ID,foodMaterial.getFoodid());
        setResult(1025,intent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG,"onResume");
        int src=getIntent().getIntExtra(SRC_CLASS,1);
        if(src==1){
           topbarHelper.setNavActivity(EditMenuActivity.class);
        }else if(src==3) {
            topbarHelper.setNavActivity(MiroEditMenuActivity.class);
        }
    }

    @Override
    protected void initData() {
        adapter.setItems(CookingMenuMangerHelper.getInstance(this,new CookingMenuDao(this)).getStdMenuFoodMaterials(""));
    }



}